﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace NotificationPusher
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        private string _subscriptionUrl;
        public string SubscriptionUrl
        {
            get
            {
                return _subscriptionUrl;
            }
            set
            {
                _subscriptionUrl = value;
                OnPropertyChanged("SubscriptionUrl");
            }
        }

        private string _title;
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                OnPropertyChanged("Title");
            }
        }

        private string _message;
        public string Message
        {
            get
            {
                return _message;
            }
            set
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }

        private string _count;
        public string Count
        {
            get
            {
                return _count;
            }
            set
            {
                _count = value;
                OnPropertyChanged("Count");
            }
        }

        private string _imageUrl;
        public string ImageUrl
        {
            get
            {
                return _imageUrl;
            }
            set
            {
                _imageUrl = value;
                OnPropertyChanged("ImageUrl");
            }
        }

        private string _status;
        public string Status
        {
            get
            {
                return _status;
            }
            set
            {
                _status = value;
                OnPropertyChanged("Status");
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        internal void SendToastNotification()
        {
            NotificationServiceReference.NotificationServiceClient client =
                new NotificationServiceReference.NotificationServiceClient();
            client.SendToastNotificationCompleted += (se, ea) =>
                {
                    if (ea.Error != null)
                    {
                        Status = "Unhandled Error";
                        return;
                    }

                    Status = ea.Result;
                };
            client.SendToastNotificationAsync(
                SubscriptionUrl,
                Title,
                Message);
        }

        internal void SendTileNotification()
        {
            NotificationServiceReference.NotificationServiceClient client =
                new NotificationServiceReference.NotificationServiceClient();
            client.SendToastNotificationCompleted += (se, ea) =>
            {
                if (ea.Error != null)
                {
                    Status = "Unhandled Error";
                    return;
                }

                Status = ea.Result;
            };
            client.SendTileNotificationAsync(
                SubscriptionUrl,
                Title,
                Count,
                ImageUrl);
        }

        internal void SendRawNotification()
        {
            NotificationServiceReference.NotificationServiceClient client =
                new NotificationServiceReference.NotificationServiceClient();
            client.SendRawNotificationCompleted += (se, ea) =>
            {
                if (ea.Error != null)
                {
                    Status = "Unhandled Error";
                    return;
                }

                Status = ea.Result;
            };
            client.SendRawNotificationAsync(
                SubscriptionUrl,
                Message);
        }
    }
}
